﻿using Iworld.basis.Service;
using Iworld.Game.Model;
using Iworld.Game.Service.Backstage.Export;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;

namespace Iworld.Game.Service.Backstage.Import
{
    /// <summary>
    /// 获取抽水统计数据集
    /// </summary>
    [DataContract]
    public class GetProfitStatisticsImport : GetPageListCheckAdministratorImportBase
    {  
        /// <summary>
        /// 开始时间
        /// </summary>
        [DataMember]
        public DateTime? BeginTime { get; set; }

        /// <summary>
        /// 结束时间
        /// </summary>
        [DataMember]
        public DateTime? EndTime { get; set; }


        public PageResult<ProfitStatisticsExport> GetProfitStatistics(IModelToDbContextOfGame db)
        {
            int pageSize = 10;
            int startRow = pageSize * (this.PageIndex - 1);
            IQueryable<GameRound> temp = db.GameRounds.Where(x => x.IsLottery == true);
            if (BeginTime != null)
            {
                temp = temp.Where(x => x.LotteryTime >= BeginTime);
            }
            if (EndTime != null)
            {
                temp = temp.Where(x => x.LotteryTime <= EndTime);
            }
            int totalCount = temp.Count();
            var list = temp.OrderByDescending(x => x.GameChapter.Id).
                OrderByDescending(x=>x.Id).Skip(startRow).Take(pageSize).ToList().ConvertAll(x =>
            {
                double allSpade = 0;
                double allHeart = 0;
                double allClub = 0;
                double allDiamond = 0;
                double allKing = 0;
                double allWin=0;
                db.Bettings.Where(k => k.GameRound.Id == x.Id).ToList().ForEach(k => {
                    allSpade += k.Spade;
                    allHeart += k.Heart;
                    allClub += k.Club;
                    allDiamond += k.Diamond;
                    allKing += k.King;
                    allWin += k.Bonus;
                });
                 return new ProfitStatisticsExport() { 
                   AllBettingGolds = (allSpade+allHeart+allClub+allDiamond+allKing),
                   AllBettingGoldsOfClub = allClub,
                   AllBettingGoldsOfDiamond = allDiamond,
                   AllBettingGoldsOfHeart = allHeart,
                   AllBettingGoldsOfKing = allKing,
                   AllBettingGoldsOfSpade = allSpade,
                    LotteryTime = (DateTime)x.LotteryTime,
                   AllWinGolds = allWin, GameChapterNumber=x.GameChapter.ChapterNumber,
                    GameRoundNumber=x.RoundNum,
                   ProfitGolds = (allSpade + allHeart + allClub + allDiamond + allKing) - allWin
                };
            });
            return new PageResult<ProfitStatisticsExport>(this.PageIndex, totalCount, pageSize, list);

            
        }
    }
}
